<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分库分表后的数据一致性保障</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .drop-cap {
            float: left;
            font-size: 3.5rem;
            line-height: 3rem;
            padding-right: 8px;
            margin-top: -4px;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 1.5rem;
            border-radius: 12px;
            margin: 2rem 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: shimmer 3s infinite;
        }
        
        @keyframes shimmer {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .tech-badge {
            display: inline-block;
            padding: 0.25rem 0.75rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 20px;
            font-size: 0.875rem;
            margin: 0.25rem;
            transition: all 0.3s ease;
        }
        
        .tech-badge:hover {
            transform: scale(1.05);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        
        .mermaid {
            margin: 2rem 0;
            text-align: center;
        }
        
        .icon-feature {
            transition: all 0.3s ease;
        }
        
        .icon-feature:hover {
            transform: translateY(-5px);
        }
        
        .icon-feature i {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 2rem;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 50%;
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            width: 2px;
            height: calc(100% + 10px);
            background: linear-gradient(to bottom, #667eea, transparent);
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-8">
        <div class="max-w-6xl mx-auto">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                分库分表后的<br>数据一致性保障
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90 leading-relaxed">
                在分布式架构的浪潮中，如何在性能与一致性之间找到完美平衡
            </p>
            <div class="flex flex-wrap gap-4">
                <span class="tech-badge"><i class="fas fa-database mr-2"></i>分布式事务</span>
                <span class="tech-badge"><i class="fas fa-sync mr-2"></i>数据同步</span>
                <span class="tech-badge"><i class="fas fa-shield-alt mr-2"></i>一致性保障</span>
                <span class="tech-badge"><i class="fas fa-network-wired mr-2"></i>高可用架构</span>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-8 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>分布式系统中，随着业务规模的扩大和并发量的增长，单一数据库逐渐成为系统的瓶颈。为了解决这个问题，分库分表技术应运而生，将数据分散到不同的库或表中，以分担负载和提高系统的扩展能力。然而，分库分表带来的好处背后却隐藏了一个巨大的挑战：数据一致性。如何在数据分散到多个物理库或表中后，确保数据操作的正确性，是设计高可用、高并发系统时必须面对的问题。
            </p>
            
            <div class="highlight-box mt-8">
                <h3 class="text-2xl font-bold mb-3 flex items-center">
                    <i class="fas fa-exclamation-triangle mr-3"></i>核心挑战
                </h3>
                <p class="text-lg relative z-10">
                    在单库中，数据库事务可以通过 ACID（原子性、一致性、隔离性、持久性）来保障一致性，但在分库分表环境中，传统的事务隔离往往失效。跨库、跨表的事务操作需要特殊的分布式一致性技术来支持。
                </p>
            </div>
        </div>

        <!-- 需求分析 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-chart-line mr-3 text-purple-600"></i>需求分析
            </h2>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="icon-feature bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-lock text-3xl mr-3"></i>
                        <h3 class="text-xl font-semibold">强一致性场景</h3>
                    </div>
                    <p class="text-gray-700">
                        业务操作的原子性要求很高，不能容忍任何状态失误。例如金融系统中的转账操作，必须确保在多个库间的资金变动是原子性和一致性的。
                    </p>
                </div>
                
                <div class="icon-feature bg-gradient-to-br from-blue-50 to-green-50 p-6 rounded-xl">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-clock text-3xl mr-3"></i>
                        <h3 class="text-xl font-semibold">最终一致性场景</h3>
                    </div>
                    <p class="text-gray-700">
                        有些业务则允许短暂的不一致性，只要数据最终能够达到一致。例如，电商系统中的订单状态更新可以通过延迟补偿或异步处理。
                    </p>
                </div>
            </div>

            <!-- 数据可视化 -->
            <div class="bg-gray-50 p-6 rounded-xl">
                <h4 class="text-lg font-semibold mb-4 text-center">一致性需求决策树</h4>
                <div class="mermaid">
                    graph TD
                        A[业务场景分析] --> B{数据一致性要求}
                        B -->|高| C[强一致性方案]
                        B -->|中| D[弱一致性方案]
                        B -->|低| E[最终一致性方案]
                        C --> F[2PC/3PC]
                        C --> G[TCC模式]
                        D --> H[读写分离]
                        D --> I[缓存策略]
                        E --> J[消息队列]
                        E --> K[异步同步]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                        style E fill:#f093fb,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </div>

        <!-- 基础设计思路 -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">
                <i class="fas fa-drafting-compass mr-3 text-purple-600"></i>基础设计思路
            </h2>
            
            <div class="space-y-6">
                <div class="timeline-item">
                    <h3 class="text-xl font-semibold mb-2">数据库分片策略</h3>
                    <p class="text-gray-700 mb-3">
                        通过水平分片和垂直分片相结合的方式，将数据合理分布到多个数据库实例中。水平分片按业务ID进行拆分，垂直分片按功能模块进行拆分。
                    </p>
                    <div class="flex flex-wrap gap-2">
                        <span class="text-sm bg-purple-100 text-purple-700 px-3 py-1 rounded-full">
                            <i class="fas fa-hashtag mr-1"></i>哈希分片
                        </span>
                        <span class="text-sm bg-purple-100 text-purple-700 px-3 py-1 rounded-full">
                            <i class="fas fa-sort-numeric-up mr-1"></i>范围分片
                        </span>
                        <span class="text-sm bg-purple-100 text-purple-700 px-3 py-1 rounded-full">
                            <i class="fas fa-layer-group mr-1"></i>一致性哈希
                        </span>
                    </div>
                </div>
                
                <div class="timeline-item">
                    <h3 class="text-xl font-semibold mb-2">事务管理机制选择</h3>
                    <p class="text-gray-700 mb-3">
                        根据业务特点选择合适的分布式事务管理方案，在性能和一致性之间找到平衡点。
                    </p>
                    <div class="grid md:grid-cols-3 gap-4">
                        <div class="bg-gradient-to-br from-purple-100 to-pink-100 p-4 rounded-lg text-center">
                            <i class="fas fa-handshake text-2xl mb-2 text-purple-600"></i>
                            <h4 class="font-semibold">2PC/3PC</h4>
                            <p class="text-sm text-gray-600">强一致性保证</p>
                        </div>
                        <div class="bg-gradient-to-br from-blue-100 to-green-100 p-4 rounded-lg text-center">
                            <i class="fas fa-code-branch text-2xl mb-2 text-blue-600"></i>
                            <h4 class="font-semibold">TCC模式</h4>
                            <p class="text-sm text-gray-600">柔性事务方案</p>
                        </div>
                        <div class="bg-gradient-to-br from-yellow-100